#!/usr/bin/env python

import utils
from errors import *

class Auth ( object ):
	def __init__ ( self, passwd ):
		super ( Auth, self ).__init__()
		self._passwd = passwd


	def authenticate ( self, username, password ):
		
		enc_pwd = self._find_user_pwd ( username )
		if not enc_pwd: return AUTH_BAD_USERNAME

		if utils.check_pwd ( password, enc_pwd, 'unix' ):
			return AUTH_OK
		else:
			return AUTH_BAD_PASSWORD


	def _find_user_pwd ( self, username ):
		fname = self._passwd
		for l in file ( fname ):
			l = l.strip()
			if not l or l[0] == '#': continue
			l = l.split ( ':' )
			if l[0] == username: return l[1]


